/*
* @Author: Flora
* @Date:   2019-01-16 09:28:10
* @Last Modified by:   Flora
* @Last Modified time: 2019-01-21 13:03:49
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/footer/index.css');
var _mm =require('util/mm.js');
var _cart = require('service/cart-service.js');
var _account = require('service/account-service.js');
var templateIndex = require('./index.string');

var page = {
	cartList : [],
	init : function(){
		var _this = this;
		_this.disp();
		_this.bindEvnt();
	},
	disp : function(){
		var _this = this;
		_cart.listCart(function(resData){
			_this.cartList = resData;
			_this.render();
		}, function(errMsg){
			alert('获取失败，请重试');
		});
	},
	bindEvnt : function(){
		var _this = this;
		$('#cart-logout').click(function(){
			// 退出购物车
			window.location.href = './index.html';
		});
		$('#create-order').click(function(){
			// 下订单
			// alert('购买');
			_this.createOrderTips();
			// _account.createOrder();
		});
	},
	render : function(){
		var _this = this;
		var cartHtml = _mm.renderHtml(templateIndex,{
			cartList : _this.cartList
		});
		$('.tb-cart').html(cartHtml);
	},
	// 确认下单提示框
	createOrderTips : function(){
		var _this = this;
		// 显示提示框
		$('.m-cart .m-winwrapper').css('display','table');
		// 监听确定按钮
		$('.m-cart .btn-mini-y').click(function(){
			$('.m-cart .m-winwrapper').css('display','none');
			_this.createOrder();
		});
		// 监听取消按钮
		$('.m-cart .btn-mini-n').click(function(){
			$('.m-cart .m-winwrapper').css('display','none');
		});
	},
	createOrder : function(){		
		_account.createOrder(function(resData){
			window.location.href='./user-account.html';
		}, function(errMsg){
			alert('订单提交失败');
		});
	}
};
$(function(){
	page.init();
});